package com.example.demo.controller;

import cn.hutool.core.map.MapUtil;
import com.example.demo.Util.HutoolNetWorkUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class RedisController {

    // 调用接口
    @GetMapping("/cc")
    public void getInterface() {
        String getCtnUnPickInfo = HutoolNetWorkUtil.hutoolGet("https://api.map.baidu.com/traffic/v1/road?" +
                "road_name=中心路&city=北京市&ak=Zyl66RULvqWvaZmSo788zNsfxs3ujqhs");

        System.out.println(getCtnUnPickInfo);
    }

//    redis
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    final static Map<String, String> map = new HashMap<>();

    static {
        map.put("hashMap1", "hashMap1");
        map.put("hashMap2", "hashMap2");
    }

    // string
    @GetMapping("/set")
    public String setRedisValue() {

        try {
            stringRedisTemplate.opsForValue().set("setValue", "这是测试数据");
            return "成功";
        } catch (Exception e) {
            return "失败";
        }
    }

    // hash
    @GetMapping("/setHash")
    public String hashRedisValue() {
        try {
            stringRedisTemplate.opsForHash().put("hashValue", "hash1", "hash1");
            stringRedisTemplate.opsForHash().putAll("hashMap", map);
            return "成功";
        } catch (Exception e) {
            return "失败";
        }
    }

    // list
    @GetMapping("/setList")
    public String setListRedisValue() {
        try {
            stringRedisTemplate.opsForList().rightPush("list", "list1");
            stringRedisTemplate.opsForList().rightPush("list", "list2");
            return "成功";
        } catch (Exception e) {
            return "失败";
        }
    }

    // set
    @GetMapping("/setSet")
    public String setSetRedisValue() {
        try {
            stringRedisTemplate.opsForSet().add("set", "1", "3", "2", "4");
            return "成功";
        } catch (Exception e) {
            return "失败";
        }
    }

    // sorted set 有序集合
    @GetMapping("/setSortedSet")
    public String setSortedSetRedisValue() {
        try {
            stringRedisTemplate.opsForZSet().add("zSet", "1", 1);
            stringRedisTemplate.opsForZSet().add("zSet", "2", 2);
            stringRedisTemplate.opsForZSet().add("zSet", "3", 3);
            return "成功";
        } catch (Exception e) {
            return "失败";
        }
    }

    @GetMapping("/setHyperRedisValue")
    public String setHyperRedisValue() {
        try {
            stringRedisTemplate.opsForHyperLogLog().add("hyper", "1", "2", "23", "3", "2", "3");
            return "成功";
        } catch (Exception e) {
            return "失败";
        }
    }

    @GetMapping("/get")
    public String getRedisValue() {
        try {
            return stringRedisTemplate.opsForValue().get("setValue");
        } catch (Exception e) {
            return "失败";
        }
    }

    @GetMapping("/getHash")
    public Object getRedisHash() {
        try {
            return stringRedisTemplate.opsForHash().get("hashMap", "hashMap2");
        } catch (Exception e) {
            System.out.println(e);
            return "失败";
        }
    }

    @GetMapping("/getList")
    public Object getListRedisValue() {
        try {
            return stringRedisTemplate.opsForList().range("list", 0, 1);
        } catch (Exception e) {
            return "失败";
        }
    }

    @GetMapping("/getSet")
    public Object getSetRedisValue() {
        try {
            return stringRedisTemplate.opsForSet().members("set");
        } catch (Exception e) {
            return "失败";
        }
    }

    @GetMapping("/getSortedSet")
    public Object getSortedSetRedisValue() {
        try {
            return stringRedisTemplate.opsForZSet().range("zSet", 0, 2);
        } catch (Exception e) {
            return "失败";
        }
    }

    @GetMapping("/getHyperRedisValue")
    public Object getHyperRedisValue() {
        try {
            return stringRedisTemplate.opsForHyperLogLog().size("hyper");
        } catch (Exception e) {
            return "失败";
        }
    }
}
